package cn.qqy.org.service.impl;

import cn.qqy.org.domain.Department;
import cn.qqy.org.domain.Employee;
import cn.qqy.org.mapper.IDepartmentMapper;
import cn.qqy.org.service.IDepartmentService;
import cn.qqy.org.utils.DepartmentQuery;
import cn.qqy.org.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private IDepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {

        departmentMapper.save(department);
        department.setCreate_time(new Date());
        if (department.getParent()==null){
            department.setPath("/"+department.getId());
        }else {
            Department findid = departmentMapper.findid(department.getId());
            department.setPath(findid.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        department.setUpdate_time(new Date());
        if (department.getParent()==null){
            department.setPath("/"+department.getId());
        }else {
            Department findid = departmentMapper.findid(department.getId());
            department.setPath(findid.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public List<Department> findall() {
        return departmentMapper.findall();
    }

    @Override
    public Department findid(Long id) {
        return departmentMapper.findid(id);
    }



    @Override
    public PageList<Department> findid1(DepartmentQuery departmentQuery) {
        //用来查询数据的条数
        Long total =  departmentMapper.count(departmentQuery);
        List<Department> list= departmentMapper.queryPage(departmentQuery);
        return new PageList<Department>( total,list);

    }

    @Override
    public List<Employee> findmanager() {
        List<Employee> findmanager = departmentMapper.findmanager();
        return findmanager;

    }
//部门
    @Override
    public List<Department> findtree() {
        return departmentMapper.findtree();
    }

    @Override
    public void batchRemove(List<Long> ids) {
        departmentMapper.batchRemove(ids);
    }
}
